﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Util.NoSQLHelpers;

namespace AllSqlHelperWin.NoSQLHelperForms
{
    public partial class RedisHelperForm : Form
    {
        public RedisHelperForm()
        {
            InitializeComponent();
        }
        private void RedisHelperForm_Load(object sender, EventArgs e)
        {
            try
            {
                RedisHelper.Initial("127.0.0.1:6379,password=ZhibiXiaobai123.");
            }
            catch (Exception ex)
            {
                MessageBox.Show("连接数据库报错！错误信息："+ ex.Message);
            }
        }

        #region String
        /// <summary>
        /// 查
        /// </summary>
        private void btn_查_Click(object sender, EventArgs e)
        {
            string DataOrder = txt查_db序号.Text.Trim();
            string Key = txt查_Key.Text.Trim();

            string Result = RedisHelper.GetStringValue_Redis(Convert.ToInt32(DataOrder), Key);
            txt查_结果.Text = Result == null ? "Null" : Result;
        }

        /// <summary>
        /// 增
        /// </summary>
        private void btn_增_Click(object sender, EventArgs e)
        {
            string DataOrder = txt增_db序号.Text.Trim();
            string Key = txt增_Key.Text.Trim();
            string Value = txt增_Value.Text.Trim();
            TimeSpan expiry = new TimeSpan(0, 0, Convert.ToInt32(txt增_过期时间.Text.Trim()));

            string Result = RedisHelper.AddStringValue_Redis(Convert.ToInt32(DataOrder), Key, Value, expiry).ToString();
            txt增_结果.Text = Result == null ? "Null" : Result;
        }

        /// <summary>
        /// 删
        /// </summary>
        private void btn_删_Click(object sender, EventArgs e)
        {
            string DataOrder = txt删_db序号.Text.Trim();
            string Key = txt删_Key.Text.Trim();

            string Result = RedisHelper.DelStringValue_Redis(Convert.ToInt32(DataOrder), Key).ToString();
            txt删_结果.Text = Result == null ? "Null" : Result;
        }
        #endregion

        #region Hash
        /// <summary>
        /// 查询
        /// </summary>
        private void button3_Click(object sender, EventArgs e)
        {
            string DataOrder = txtHashdb_查.Text.Trim();
            string Key = txtHashKey_查.Text.Trim();
            string FieId = txtHashFieId_查.Text.Trim();

            string Result = RedisHelper.GetHashValue_Redis(Convert.ToInt32(DataOrder), Key, FieId);
            txtHashResult_查.Text = Result == null ? "Null" : Result;
        }
        /// <summary>
        /// 获取所有值
        /// </summary>
        private void button13_Click(object sender, EventArgs e)
        {
            string DataOrder = txtHashdb_查.Text.Trim();
            string Key = txtHashKey_查.Text.Trim();

            List<string> Result = RedisHelper.GetAllHashValue_Redis(Convert.ToInt32(DataOrder), Key);

            txtHashResult_查.Text = Result == null ? "Null" : string.Join(",", Result);
        }
        /// <summary>
        /// 添加
        /// </summary>
        private void button2_Click(object sender, EventArgs e)
        {
            string DataOrder = txtHashdb_增.Text.Trim();
            string Key = txtHashKey_增.Text.Trim();
            string FieId = txtHashFieId_增.Text.Trim();
            string Value = txtHashValue_增.Text.Trim();

            string Result = RedisHelper.AddHashValue_Redis(Convert.ToInt32(DataOrder), Key, FieId, Value).ToString();
            txtHashResult_增.Text = Result == null ? "Null" : Result;
        }
        /// <summary>
        /// 删除HashKey
        /// </summary>
        private void button1_Click(object sender, EventArgs e)
        {
            string DataOrder = txtHashdb_删.Text.Trim();
            string Key = txtHashKey_删.Text.Trim();
            string FieId = txtHashFieId_删.Text.Trim();

            string Result = RedisHelper.DelHashKeyValue_Redis(Convert.ToInt32(DataOrder), Key, FieId).ToString();
            txtHashResult_删.Text = Result == null ? "Null" : Result;
        }

        #endregion Hash

        #region Set
        /// <summary>
        /// 查询所有
        /// </summary>
        private void button14_Click(object sender, EventArgs e)
        {
            string DataOrder = txtSetdb_查.Text.Trim();
            string Key = txtSetKey_查.Text.Trim();

            List<string> Result = RedisHelper.GetSetValue_Redis(Convert.ToInt32(DataOrder), Key);
            txtSetResult_查.Text = Result == null ? "Null" : string.Join(",", Result);
        }
        /// <summary>
        /// 增
        /// </summary>
        private void button5_Click(object sender, EventArgs e)
        {
            string DataOrder = txtSetdb_增.Text.Trim();
            string Key = txtSetKey_增.Text.Trim();
            string Value = txtSetValue_增.Text.Trim();

            string Result = RedisHelper.AddSetValue_Redis(Convert.ToInt32(DataOrder), Key, Value).ToString();
            txtSetResult_增.Text = Result == null ? "Null" : Result;
        }
        /// <summary>
        /// 删
        /// </summary>
        private void button4_Click(object sender, EventArgs e)
        {
            string DataOrder = txtSetdb_删.Text.Trim();
            string Key = txtSetKey_删.Text.Trim();
            string Value = txtSetValue_删.Text.Trim();

            string Result = RedisHelper.DelSetKeyValue_Redis(Convert.ToInt32(DataOrder), Key, Value).ToString();
            txtSetResult_删.Text = Result == null ? "Null" : Result;
        }

        #endregion Set

        #region ZSet
        /// <summary>
        /// 查
        /// </summary>
        private void button9_Click(object sender, EventArgs e)
        {
            string DataOrder = txtZSetdb_查.Text.Trim();
            string Key = txtZSetKey_查.Text.Trim();

            List<ZsetValue> Result = RedisHelper.GetZSetValue_Redis(Convert.ToInt32(DataOrder), Key);
            txtZSetResult_查.Text = Result == null ? "Null" : string.Join(";", Result.Select(s => "值:" + s.Value + "排序级别:" + s.Order).ToList<string>());
        }
        /// <summary>
        /// 增
        /// </summary>
        private void button8_Click(object sender, EventArgs e)
        {
            string DataOrder = txtZSetdb_增.Text.Trim();
            string Key = txtZSetKey_增.Text.Trim();
            string Value = txtZSetValue_增.Text.Trim();
            string Order = txtZSetOrder_增.Text.Trim();

            string Result = RedisHelper.AddZSetValue_Redis(Convert.ToInt32(DataOrder), Key, Value, Convert.ToInt32(Order)).ToString();
            txtZSetResult_增.Text = Result == null ? "Null" : Result;
        }
        /// <summary>
        /// 删
        /// </summary>
        private void button7_Click(object sender, EventArgs e)
        {
            string DataOrder = txtZSetdb_删.Text.Trim();
            string Key = txtZSetKey_删.Text.Trim();
            string Value = txtZSetValue_删.Text.Trim();

            string Result = RedisHelper.DelZSetKeyValue_Redis(Convert.ToInt32(DataOrder), Key, Value).ToString();
            txtZSetResult_删.Text = Result == null ? "Null" : Result;
        }
        #endregion ZSet

        #region List
        /// <summary>
        /// 查
        /// </summary>
        private void button12_Click(object sender, EventArgs e)
        {
            string DataOrder = txtListdb_查.Text.Trim();
            string Key = txtListKey_查.Text.Trim();

            List<string> Result = RedisHelper.GetListValue_Redis(Convert.ToInt32(DataOrder), Key);
            txtListResult_查.Text = Result == null ? "Null" : string.Join(",", Result);
        }
        /// <summary>
        /// 增
        /// </summary>
        private void button11_Click(object sender, EventArgs e)
        {
            string DataOrder = txtListdb_增.Text.Trim();
            string Key = txtListKey_增.Text.Trim();
            string Value = txtListValue_增.Text.Trim();
            bool IsTop = radListTop_增.Checked;

            string Result = RedisHelper.AddListValue_Redis(Convert.ToInt32(DataOrder), Key, Value, IsTop).ToString();
            txtListResult_增.Text = Result == null ? "Null" : Result;
        }
        /// <summary>
        /// 删
        /// </summary>
        private void button10_Click(object sender, EventArgs e)
        {
            string DataOrder = txtListdb_删.Text.Trim();
            string Key = txtListKey_删.Text.Trim();
            bool IsTop = radListTop_删.Checked;

            string Result = RedisHelper.DelListKeyValue_Redis(Convert.ToInt32(DataOrder), Key, IsTop).ToString();
            txtListResult_删.Text = Result == null ? "Null" : Result;
        }
        #endregion List
    }
}
